package com.slz3.business.module.wmsManagement.wmsStockDetailItems.steward;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.wmsManagement.wmsStockDetailItems.domain.WmsStockDetailItems;
import com.slz3.business.module.wmsManagement.wmsStockDetailItems.domain.WmsStockDetailItemsCondition;
import org.springframework.web.reactive.function.server.ServerRequest;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

public class WmsStockDetailItemsResSafeValid {

    static boolean add(ServerRequest req, WmsStockDetailItems wmsStockDetailItems) {
        // T-O-D-O 唯一约束
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean delete(ServerRequest req, WmsStockDetailItemsCondition wmsStockDetailItemsCondition) {
        List<WmsStockDetailItems> wmsStockDetailItemsList = Services.wmsStockDetailItems.findAllByUuids(wmsStockDetailItemsCondition.getUuidList());
        if (wmsStockDetailItemsList.stream().filter(
                item -> AuditFlag.isApproved(item.getSerAudit()) || AuditFlag.isReject(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能删除【已审核】或【被驳回】或【被删除】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, wmsStockDetailItemsList);
        }
        return RR.hasResErr(req);
    }

    static boolean modify(ServerRequest req, WmsStockDetailItems wmsStockDetailItems) {
        Optional<WmsStockDetailItems> wmsStockDetailItemsDB = Services.wmsStockDetailItems.single(wmsStockDetailItems.getUuid());
        if (wmsStockDetailItemsDB.isPresent()) {
            req.attributes().put(RR.RELAY_SAFE_VALID, wmsStockDetailItemsDB.get());
        } else {
            RR.resDetail(req).put(RR.SAFE_VALID, "cannot find entity by request uuid");
        }
        return RR.hasResErr(req);
    }

    static boolean info(ServerRequest req) {
        Optional<WmsStockDetailItems> wmsStockDetailItemsDB = Services.wmsStockDetailItems.single(UUID.fromString(req.queryParam("uuid").get()));
        if (!wmsStockDetailItemsDB.isPresent()) {
            RR.resDetail(req).put(RR.SAFE_VALID, "cannot find entity by request uuid");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, wmsStockDetailItemsDB.get());
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean auditSubmit(ServerRequest req, WmsStockDetailItemsCondition wmsStockDetailItemsCondition) {
        List<WmsStockDetailItems> wmsStockDetailItemsList = Services.wmsStockDetailItems.findAllByUuids(wmsStockDetailItemsCondition.getUuidList());
        if (wmsStockDetailItemsList.stream().filter(item -> ValidFlag.isValid(item.getValidFlag())
                && (AuditFlag.isWaitSubmit(item.getSerAudit()) || AuditFlag.isReject(item.getSerAudit()))
        ).count() > 0) {
            req.attributes().put(RR.RELAY_SAFE_VALID, wmsStockDetailItemsList);
        } else {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能提交【被删除】状态或不是【待提交】或【驳回】状态的记录");
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean audit(ServerRequest req, WmsStockDetailItemsCondition wmsStockDetailItemsCondition) {
        List<WmsStockDetailItems> wmsStockDetailItemsList = Services.wmsStockDetailItems.findAllByUuids(wmsStockDetailItemsCondition.getUuidList());
        if (wmsStockDetailItemsList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能审核【被删除】状态或不是【待审核】状态的记录");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, wmsStockDetailItemsList);
        }
        return RR.hasResErr(req);
    }

    /**
     * known: params uuids is not null
     */
    static final boolean auditReject(ServerRequest req, WmsStockDetailItemsCondition wmsStockDetailItemsCondition) {
        List<WmsStockDetailItems> wmsStockDetailItemsList = Services.wmsStockDetailItems.findAllByUuids(wmsStockDetailItemsCondition.getUuidList());
        if (wmsStockDetailItemsList.stream().filter(item -> ValidFlag.isValid(item.getValidFlag())
                && (AuditFlag.isWaitAudit(item.getSerAudit()) || AuditFlag.isApproved(item.getSerAudit()))
        ).count() > 0) {
            req.attributes().put(RR.RELAY_SAFE_VALID, wmsStockDetailItemsList);
        } else {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能驳回【被删除】状态或不是【待审核】或【已审核】状态的记录");
        }
        return RR.hasResErr(req);
    }


    static final boolean usingEnable(ServerRequest req, WmsStockDetailItemsCondition wmsStockDetailItemsCondition) {
        List<WmsStockDetailItems> wmsStockDetailItemsList = Services.wmsStockDetailItems.findAllByUuids(wmsStockDetailItemsCondition.getUuidList());
        if (wmsStockDetailItemsList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能启用【被删除】状态");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, wmsStockDetailItemsList);
        }
        return RR.hasResErr(req);
    }


    static final boolean usingDisable(ServerRequest req, WmsStockDetailItemsCondition wmsStockDetailItemsCondition) {
        List<WmsStockDetailItems> wmsStockDetailItemsList = Services.wmsStockDetailItems.findAllByUuids(wmsStockDetailItemsCondition.getUuidList());
        if (wmsStockDetailItemsList.stream().filter(
                item -> !AuditFlag.isWaitAudit(item.getSerAudit()) || ValidFlag.isDel(item.getValidFlag())
        ).count() > 0) {
            RR.resDetail(req).put(RR.SAFE_VALID, "不能禁用【被删除】状态");
        } else {
            req.attributes().put(RR.RELAY_SAFE_VALID, wmsStockDetailItemsList);
        }
        return RR.hasResErr(req);
    }
}
